import './utils/public-path'
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import './assets/main.css'

// 导入消息队列管理器
import { interceptElMessage } from './utils/messageQueue'

// qiankun微前端相关设置
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'
import { useAuthStore } from './store/auth'

// 创建Vue实例
let app: any;

/**
 * 独立运行时
 */
async function mount() {
  app = createApp(App);
  app.use(createPinia());
  
  // 获取auth store
  const authStore = useAuthStore();
  
  // 初始化：检查token
  await authStore.initialize();
  
  app.use(router);
  app.use(ElementPlus);
  
  // 启用消息队列拦截
  interceptElMessage();
  
  app.mount('#app');
}

/**
 * qiankun生命周期
 */
renderWithQiankun({
  /**
   * 子应用挂载
   */
  async mount(props: any) {
    app = createApp(App);
    app.use(createPinia());
    
    // 获取auth store
    const authStore = useAuthStore();
    
    // 初始化：检查token
    await authStore.initialize();
    
    app.use(router);
    app.use(ElementPlus);
    
    // 启用消息队列拦截
    interceptElMessage();
    
    // 确保挂载目标存在
    const container = props.container;
    if (container) {
      app.mount(container.querySelector('#app') || container);
    } else {
      app.mount('#app');
    }
  },
  
  /**
   * 子应用启动
   */
  bootstrap() {
  },
  
  /**
   * 子应用卸载
   */
  unmount() {
    app?.unmount();
  },
  
  /**
   * 子应用更新
   */
  update() {
  }
});

// 独立运行时直接挂载
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  mount();
} 